package com.xxx.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xxx.config.Request;
import com.xxx.pojo.Department;
import com.xxx.pojo.User;
import com.xxx.pojo.vo.DepartmentVo;
import com.xxx.service.DepartmentService;
import com.xxx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author George
 * @since 2023-07-04
 */
@Api(tags = "部门控制")
@RestController
@CrossOrigin
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UserService userService;

    /**
     * 所有查询部门
     *
     * @return 返回操作结果
     */
    @ApiOperation(value = "查询部门", notes = "所有查询部门")
    @GetMapping("/departmentList")
    public Request departmentList () {
        List<Department> list = departmentService.list();
        return list.size() > 0 ? Request.suc(list) : Request.error("查询部门失败");
    }


    /**
     * 添加新部门
     *
     * @param department 部门信息
     * @return 返回操作结果
     */
    @ApiOperation(value = "添加新部门", notes = "添加新部门")
    @PostMapping("/addDepartment")
    public Request addDepartment(@Valid @RequestBody Department department) {
        boolean saved = departmentService.save(department);
        return saved ? Request.suc() : Request.error("添加部门失败");
    }


    /**
     * 修改部门信息
     *
     * @param departmentId   部门id
     * @param newDepartmentName 部门名称
     * @return 返回操作结果
     */
    @ApiOperation(value = "修改部门名称", notes = "修改部门名称")
    @PutMapping("/updateDepartmentName")
    public Request updateDepartmentName(
            @RequestParam("departmentId") Integer departmentId,
            @RequestParam("newDepartmentName") String newDepartmentName) {

        Department department = departmentService.getById(departmentId);
        if (department == null) {
            return Request.error("指定的部门不存在");
        }

        department.setDepartmentName(newDepartmentName);
        boolean updated = departmentService.updateById(department);
        return updated ? Request.suc() : Request.error("修改部门信息失败");
    }


    /**
     * 连表根据部门查询
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据条件查询用户列表", notes = "根据分页查询参数查询用户列表")
    @PostMapping("/queryDepartment")
    public Request queryDepartment(
            @RequestParam("searchValue") String searchValue,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        Page<DepartmentVo> mapPage = new Page<>(pageNum, pageSize, true);

        // 去除查询条件中的空格
        String finalSearchValue = StringUtils.isNotBlank(searchValue) ? searchValue.trim() : null;

        MPJLambdaWrapper<Department> lambdaWrapper = new MPJLambdaWrapper<Department>()
                .select(Department::getDepartmentName)
                .select(User::getName,User::getDeleted,User::getUserName,
                        User::getPhone,User::getPerms,User::getWorkNumber,
                        User::getGmtCreation,User::getIp)
                .leftJoin(User.class, User::getDepartmentId, Department::getDepartmentId);

        if (StringUtils.isNotBlank(finalSearchValue)) {
            lambdaWrapper.and(wrapper -> wrapper
                    .like(Department::getDepartmentId, finalSearchValue));
        }

        departmentService.selectJoinListPage(mapPage,DepartmentVo.class, lambdaWrapper);

        // 使用PageInfo对象包装查询结果
        List<DepartmentVo> records = mapPage.getRecords();

        // 获取总记录数
        long total = mapPage.getTotal();

        return records.size() > 0 ? Request.suc(total,records) : Request.error("查询用户失败");
    }

}

